<?php
namespace app\serverfund\logic;

use \app\manage\logic\BaseLogic;
/*使用支付宝类*/
use alipay\ClientResponseHandler;
use alipay\PayHttpClient;
use alipay\RequestHandler;
use alipay\Utils;

/**
 * @author:      李颖
 * @DateTime:    2017-09-08 10:25:59
 * @Description: 原始银联代码   来源：测试案例中的request.php文件修改而来
 */
class RequestUnionpay extends BaseLogic
{

	private $resHandler = null;
    private $reqHandler = null;
    private $pay = null;
    private $cfg = null;

    public function __construct(){
        $this->Request();
    }

    public function Request(){
        $this->resHandler = new ClientResponseHandler();
        $this->reqHandler = new RequestHandler();
        $this->pay = new PayHttpClient();
        $this->cfg = config('weiFortisPay');

        $this->reqHandler->setGateUrl($this->cfg['url']);  //读取配置
        $this->reqHandler->setKey($this->cfg['key']);
    }


    /**
     * 提交订单信息
     */
    public function submitOrderInfo($paramData){
        $nonce_str = mt_rand(NOW, NOW + rand());
        $this->reqHandler->setReqParams($paramData);
        $this->reqHandler->setParameter('service','pay.unionpay.native');//接口类型   注：与支付宝不同点1
        $this->reqHandler->setParameter('mch_id',$this->cfg['mchId']);//必填项，商户号，由平台分配
        $this->reqHandler->setParameter('notify_url',$this->cfg['notify_url']);
        $this->reqHandler->setParameter('version',$this->cfg['version']);
        $this->reqHandler->setParameter('nonce_str',$nonce_str);//随机字符串，必填项，不长于 32 位
        $this->reqHandler->createSign();//创建签名
        $data = Utils::toXml($this->reqHandler->getAllParameters());
        $this->pay->setReqContent($this->reqHandler->getGateURL(),$data);
        if(!$this->pay->call()){
            $payRes = [
                'status' => 500,
                'msg' => 'Response Code:' . $this->pay->getResponseCode()
                    .' Error Info:' . $this->pay->getErrInfo()
            ];
            return auto_failure($payRes, $this->pay->getErrInfo());
        }
        // 设置要发送的内容
        $this->resHandler->setContent($this->pay->getResContent());
        $this->resHandler->setKey($this->reqHandler->getKey());
        // 如果签名验证通过
        if(!$this->resHandler->isTenpaySign()){
            $payRes = [
                'status'=>500,
                'msg'=>'Error Code:' . $this->resHandler->getParameter('status')
                        . ' Error Message:' . $this->resHandler->getParameter('message')
            ];
            return auto_failure($payRes, $this->resHandler->getParameter('message'));
        }
        if($this->resHandler->getParameter('status') != 0 || $this->resHandler->getParameter('result_code') != 0){
            $payRes = [
                'status' => 500,
                'msg' => 'Error Code:' . $this->resHandler->getParameter('err_code')
                        . ' Error Message:' . $this->resHandler->getParameter('err_msg')
            ];
            return auto_failure($payRes, $this->resHandler->getParameter('err_msg'));
        }
        $payRes = [
            'code_img_url' => $this->resHandler->getParameter('code_img_url'),
            'code_url' => $this->resHandler->getParameter('code_url'),
            'sign' => $this->resHandler->getParameter('sign'),
            'nonce_str' => $this->resHandler->getParameter('nonce_str')
        ];
        //此方法为中间调用方法，不直接返回,在此可返回sign nonce_str
        return auto_success($payRes);
    }
   






}